Skip to content

Conversation

@chandra-siri
Copy link
Collaborator

fix(experimental): implement requests_done method to signal end of requests in async streams. Gracefully close streams.

…quests in async streams. Gracefully close streams.
@chandra-siri chandra-siri requested review from a team as code owners January 12, 2026 14:47
@chandra-siri chandra-siri enabled auto-merge (squash) January 12, 2026 14:47
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: storage Issues related to the googleapis/python-storage API. labels Jan 12, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @chandra-siri, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the robustness of experimental asynchronous read and write object streams by implementing a mechanism to explicitly signal the end of client requests. This change ensures that the bidi-gRPC connection is closed gracefully, preventing potential issues where the server might be left waiting for further client input. The addition of dedicated unit tests validates this new behavior, contributing to the overall stability of the async stream handling.

Highlights

  • New requests_done method: Introduced a new asynchronous method requests_done in both async_read_object_stream.py and async_write_object_stream.py to explicitly signal the completion of client-side requests in a bidi-gRPC stream.
  • Graceful Stream Closure: The close method in both async stream implementations now calls requests_done before closing the underlying gRPC connection, ensuring a more graceful termination of the stream.
  • Unit Test Coverage: Added new unit tests and updated existing ones to cover the functionality of the requests_done method and its integration into the stream closure process for both read and write object streams.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a requests_done method to gracefully close asynchronous streams for reading and writing objects. The implementation correctly signals the end of requests to the server before closing the connection. My review focuses on improving code quality and test coverage. I've suggested adding guard clauses to the new methods for robustness and refactoring duplicated code into a base class to improve maintainability. I've also recommended adding tests for the failure scenarios of the new methods.

Comment on lines +158 to +162
async def requests_done(self):
"""Signals that all requests have been sent."""

await self.socket_like_rpc.send(None)
await self.socket_like_rpc.recv()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This new method has a couple of areas for improvement:

  1. Missing guard clause: For consistency with other methods like send and recv, a check should be added to ensure the stream is open before proceeding. This prevents errors if the method is called on a closed stream.
  2. Code duplication: This method is identical to the one in _AsyncWriteObjectStream. To improve maintainability, consider moving this shared logic to the _AsyncAbstractObjectStream base class. This would likely involve moving _is_stream_open and socket_like_rpc to the base class as well.

Here is a suggested implementation that includes the guard clause:

    async def requests_done(self):
        """Signals that all requests have been sent."""
        if not self._is_stream_open:
            raise ValueError("Stream is not open")

        await self.socket_like_rpc.send(None)
        await self.socket_like_rpc.recv()

Comment on lines +159 to +163
async def requests_done(self):
"""Signals that all requests have been sent."""

await self.socket_like_rpc.send(None)
await self.socket_like_rpc.recv()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to my comment on async_read_object_stream.py, this method should include a check to ensure the stream is open.

Additionally, this logic is duplicated across both stream classes. Refactoring this into the _AsyncAbstractObjectStream base class would be a good improvement for maintainability.

    async def requests_done(self):
        """Signals that all requests have been sent."""
        if not self._is_stream_open:
            raise ValueError("Stream is not open")

        await self.socket_like_rpc.send(None)
        await self.socket_like_rpc.recv()

"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"
)
@pytest.mark.asyncio
async def test_requests_done(mock_client, mock_cls_async_bidi_rpc):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a good test for the happy path. With the recommended addition of the _is_stream_open check in requests_done, it would be beneficial to also add a test case to verify that a ValueError is raised when requests_done is called on a closed stream, similar to test_close_without_open_should_raise_error.

@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_requests_done(mock_cls_async_bidi_rpc, mock_client):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the read stream test, this is a good happy path test. It would be valuable to add another test to ensure requests_done raises a ValueError when the stream is not open, to cover the failure case. This would be analogous to test_close_without_open_should_raise_error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the googleapis/python-storage API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants